const fs = require('fs')
//promisfy可以把异步api转换成promise
const { promisify } = require('util')
const path = require('path')
//读异步
const readFile = promisify(fs.readFile)
//写异步
const writeFile = promisify(fs.writeFile)

const dbPath = path.join(__dirname,'./db.json')
exports.getDb = async() =>{
  const data = await readFile(dbPath,'utf8')
  return JSON.parse(data)
}

exports.saveDb = async db => {
  const data = JSON.stringify(db,null , '  ')
  await writeFile(dbPath,data)
}